/** 
 * 自动建hacknet
 * 自动买自动升级
 * 直到买的收益过少为止
 */
import {showNumberIn6} from "/stock/kelly4s.js"


function calc_and_upgrade(ns)
{

  ns.print("===========================");
  let numNodes = ns.hacknet.numNodes();
  ns.print("numNodes\t:\t",numNodes);
  let numHashes = ns.hacknet.numHashes()
  ns.print("numHashes\t:\t",numHashes);
  let maxNumNodes = ns.hacknet.maxNumNodes();
  ns.print("maxNumNodes\t:\t",maxNumNodes);
  let CacheUpgradeCost = ns.hacknet.getCacheUpgradeCost(0,1);
  ns.print("CacheUpgradeCost\t:\t",CacheUpgradeCost);
  let CoreUpgradeCost = ns.hacknet.getCoreUpgradeCost(0, 1);
  ns.print("CoreUpgradeCost\t:\t",CoreUpgradeCost);
  let getHashUpgrades = ns.hacknet.getHashUpgrades()
  ns.print("getHashUpgrades\t:\t",getHashUpgrades);
  let levelUpgradeCost = ns.hacknet.getLevelUpgradeCost(0, 1);
  ns.print("getHashUpgrades\t:\t",levelUpgradeCost);
  let getPurchaseNodeCost = ns.hacknet.getPurchaseNodeCost(0);
  ns.print("getPurchaseNodeCost\t:\t",getPurchaseNodeCost);
  let getStudyMult = ns.hacknet.getStudyMult(0);
  ns.print("getStudyMult\t:\t",getStudyMult);
  let getTrainingMult = ns.hacknet.getTrainingMult()
  ns.print("getTrainingMult\t:\t",getTrainingMult);
  let getNodeStats = ns.hacknet.getNodeStats(0);
  ns.print("getNodeStats:");
  for(let key in getNodeStats)
  {
    ns.print("\t\t",key,"\t:\t",getNodeStats[key]);
  }
  ns.print("===========================");
  // 遍历所有的服务器:
  // 计算最小升级费用.
  let min_upgrade_cost = getPurchaseNodeCost;// 买台服务器应该是最贵的吧.
  let id_of_node = -1;
  let upgrade_type = 0;// 升级项目: 0:买服务器, 1:升等级, 2:升内存 3:升cpu
  let total_production = 0;// 总的每秒收入
  for(let i=0;i<numNodes;++i)
  {
    let nodeStats = ns.hacknet.getNodeStats(i);
    total_production+=nodeStats["production"];
    let level_upgrade_cost = ns.hacknet.getLevelUpgradeCost(i);
    if(level_upgrade_cost < min_upgrade_cost )
    {
      min_upgrade_cost = level_upgrade_cost;
      id_of_node = i;
      upgrade_type = 1;
    }
    let ram_upgrade_cost = ns.hacknet.getRamUpgradeCost(i);
    if(ram_upgrade_cost < min_upgrade_cost)
    {
      min_upgrade_cost = ram_upgrade_cost;
      id_of_node = i;
      upgrade_type = 2;
    }
    let core_upgrade_cost = ns.hacknet.getCoreUpgradeCost(i);
    if(core_upgrade_cost < min_upgrade_cost)
    {
      min_upgrade_cost = core_upgrade_cost;
      id_of_node = i;
      upgrade_type = 3;
    }
  }
  ns.print("每秒收入:",showNumberIn6(total_production));
  ns.print("赚够5.2b第一桶金需要:",(5200000000/total_production) / 3600,"小时");
  // ns.print("计划:");
  // ns.print("\t\t",min_upgrade_cost);
  // ns.print("\t\t",id_of_node);
  // ns.print("\t\t",upgrade_type);
  // ns.print("\t\tmin_upgrade_cost:",min_upgrade_cost,"\t,\ttotal_production*3600:",total_production*3600);

  // 要是升级后一小时能赚回来, 那就升级
  if(min_upgrade_cost < total_production*3600)
  {
    switch(upgrade_type)
    {
    case 0:
      ns.hacknet.purchaseNode();
      break;
    case 1:
      ns.print("升级等级:目标机器:",id_of_node)
      ns.hacknet.upgradeLevel(id_of_node);
      break;
    case 2:
      ns.print("升级内存:目标机器:",id_of_node)
      ns.hacknet.upgradeRam(id_of_node);
      break;
    case 3:
      ns.print("升级core:目标机器:",id_of_node)
      ns.hacknet.upgradeCore(id_of_node);
      break;
    }
  }
  else
  {
    ns.tprint("hacknet 服务器价太高");
  }
  
}

export async function main(ns) {

  // var a= ns.hacknet.getCacheUpgradeCost(0, 1);
  // ns.print(a);

  while(true)
  {
    calc_and_upgrade(ns)
    await ns.sleep(1000);
  }
}